/**
 * 2019.8.26 按钮权限指令
 * 拿到全局store中存储的用户权限列表 btnList
 * 通过btnList的id的比对,进行按钮的权限控制
 */

import store from '@/store'

export default {
  inserted(el, binding, vnode) {
    const { value } = binding
    const btnList = store.getters && store.getters.btnList

    if (value && value instanceof Array && value.length > 0) {
      const permissonBtn = value

      const hasPermission = btnList.some(btn => {
        return permissonBtn.includes(btn.id)
      })

      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error('need BtnList, Like v-permission="[]')
    }
  }
}

